// Copyright 2013 Square, Inc.

package com.squareup.timessquare;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.Html;
import android.util.AttributeSet;
import android.widget.TextView;

import com.squareup.timessquare.MonthCellDescriptor.RangeState;

public class CalendarCellView extends TextView {
  private static final int[] STATE_SELECTABLE = {
      R.attr.state_selectable
  };
  private static final int[] STATE_CURRENT_MONTH = {
      R.attr.state_current_month
  };
  private static final int[] STATE_TODAY = {
      R.attr.state_today
  };
  private static final int[] STATE_HIGHLIGHTED = {
      R.attr.state_highlighted
  };
  private static final int[] STATE_RANGE_FIRST = {
      R.attr.state_range_first
  };
  private static final int[] STATE_RANGE_MIDDLE = {
      R.attr.state_range_middle
  };
  private static final int[] STATE_RANGE_LAST = {
      R.attr.state_range_last
  };
  private static final int[] STATE_HOLIDAYS = {
      R.attr.state_holidays
  };

  private boolean isSelectable = false;
  private boolean isCurrentMonth = false;
  private boolean isToday = false;
  private boolean isTomorrow = false;
  private boolean isDayAfterTomorrow = false;
  private boolean isHighlighted = false;
  private boolean isHoliDays = false;
  private RangeState rangeState = RangeState.NONE;

 
  public CalendarCellView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void setSelectable(boolean isSelectable) {
    this.isSelectable = isSelectable;
    refreshDrawableState();
  }

  public void setCurrentMonth(boolean isCurrentMonth) {
    this.isCurrentMonth = isCurrentMonth;
    refreshDrawableState();
  }

  public void setToday(boolean isToday) {
    this.isToday = isToday;
    refreshDrawableState();
  }
  
  public void setTomorrow(boolean isTomorrow) {
	this.isTomorrow = isTomorrow;
	refreshDrawableState();
  }
  public void setDayAfterTomorrow(boolean isDayAfterTomorrow) {
	this.isDayAfterTomorrow = isDayAfterTomorrow;
	refreshDrawableState();
 }

  public void setRangeState(MonthCellDescriptor.RangeState rangeState) {
    this.rangeState = rangeState;
    refreshDrawableState();
  }

  public void setHighlighted(boolean highlighted) {
    isHighlighted = highlighted;
    refreshDrawableState();
  }

  public void setHoliDays(boolean isHoliDays) {
	this.isHoliDays = isHoliDays;
	refreshDrawableState();
}
  	@Override
	public void setText(CharSequence text, BufferType type) {
  		String s=text.toString();
  		String ss[]=s.split("\n");
  		String redcor="#C0C0C0";
  		if (ss.length>1) {
  			String a=ss[0];
  			if (!isNumeric(a) && !isTomorrow && !isDayAfterTomorrow ) { 
				redcor = "#FF0000";
  			}
  			super.setText(Html.fromHtml(a+"<br><font color='"+redcor+"'>"+ss[1]+"</font>"), type);
		}else { 
			super.setText(text, type);
		}
			
	}
  
  	private static boolean isNumeric(String str){
  	   Pattern pattern = Pattern.compile("[0-9]*");
  	   Matcher isNum = pattern.matcher(str);
  	   if( !isNum.matches() ){
  	       return false;
  	   }
  	   return true;
  	}
  
  @Override protected int[] onCreateDrawableState(int extraSpace) {
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 5);

    if (isSelectable) {
      mergeDrawableStates(drawableState, STATE_SELECTABLE);
    }

    if (isCurrentMonth) {
      mergeDrawableStates(drawableState, STATE_CURRENT_MONTH);
    }

    if (isToday) {
        mergeDrawableStates(drawableState, STATE_TODAY);
    }
    
    if (isTomorrow) {
        mergeDrawableStates(drawableState, STATE_TODAY);
     }
    
    if (isDayAfterTomorrow) {
        mergeDrawableStates(drawableState, STATE_TODAY);
     }

    if (isHighlighted) {
        mergeDrawableStates(drawableState, STATE_HIGHLIGHTED);
     }
    if (isHoliDays) {
        mergeDrawableStates(drawableState, STATE_HOLIDAYS);
     }

    if (rangeState == MonthCellDescriptor.RangeState.FIRST) {
      mergeDrawableStates(drawableState, STATE_RANGE_FIRST);
    } else if (rangeState == MonthCellDescriptor.RangeState.MIDDLE) {
      mergeDrawableStates(drawableState, STATE_RANGE_MIDDLE);
    } else if (rangeState == RangeState.LAST) {
      mergeDrawableStates(drawableState, STATE_RANGE_LAST);
    }

    return drawableState;
  }
}
